/**
 * OSAmI: Open Source Ambient Intelligence
 * Copyright (C) 2010 Bull S.A.S.
 * Contact: osami-general@osami-commons.org
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307
 * USA
 */

package org.osami.gateway.identity.naive.internal;

import org.osami.gateway.identity.Identity;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;

import java.util.HashMap;
import java.util.Map;

import static org.testng.Assert.*;

/**
 * Created by IntelliJ IDEA.
 * User: guillaume
 * Date: 29 juil. 2010
 * Time: 11:49:44
 * To change this template use File | Settings | File Templates.
 */
public class IdentityManagerTestCase {

    private NaiveIdentityManager manager;

    @BeforeMethod
    private void setUp() throws Exception {
        manager = new NaiveIdentityManager();
    }

    @Test
    public void testCreateEmptyIdentity() throws Exception {
        Identity id = manager.createIdentity(null);
        assertNotNull(id);
        assertEquals(id.getName(), "<No name provided>", "default name is not right");
        assertNull(id.getBoundIdentifier(), "no bound identifier for a new Identity");
        assertNotNull(id.getProperties(), "properties are not available");
        assertEquals(id.getProperties().size(), 1, "Should only have the name in the properties");
    }

    @Test
    public void testCreateIdentityIDUnicity() throws Exception {
        Identity id1 = manager.createIdentity(null);
        assertNotNull(id1);
        Identity id2 = manager.createIdentity(null);
        assertNotNull(id2);

        assertFalse(id1.getIdentityID().equals(id2.getIdentityID()));
    }

    @Test
    public void testCreateInheritedIdentity() throws Exception {
        Map<String, Object> props = new HashMap<String, Object>();
        props.put(Identity.FRIENDLY_NAME, "Parent");
        props.put("in.parent", "YES");

        Identity parent = manager.createIdentity();
        assertNotNull(parent);
        parent.putProperties(props);

        Identity child = manager.createIdentity(parent);
        assertNotNull(child);

        assertEquals(child.getMergedProperties().get("in.parent"), "YES");
    }

    @Test
    public void testRemoveIdentity() throws Exception {
        Identity one = manager.createIdentity(null);
        Identity two = manager.createIdentity(null);

        assertEquals(manager.getIdentities(null).size(), 2);

        manager.removeIdentity(one.getIdentityID());

        assertEquals(manager.getIdentities(null).size(), 1);

        manager.removeIdentity(two.getIdentityID());

        assertEquals(manager.getIdentities(null).size(), 0);
    }

    @Test
    public void testIdentityBindingUnbinding() throws Exception {
        Identity one = manager.createIdentity(null);
        manager.bind("first-binding", one);

        Identity prime = manager.getBoundIdentity("first-binding");
        assertSame(prime, one);

        manager.unbind("first-binding");
        Identity second = manager.getBoundIdentity("first-binding");
        assertNull(second);
    }
}
